<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Validate;
use think\Config;
use app\common\controller\AdminBase;

class System extends AdminBase{
	
	//网站配置
    public function siteconfig(){
       $site_config = Db::name('system')->field('value')->where('name', 'site_config')->find();
        $site_config = unserialize($site_config['value']);
        return $this->fetch('siteconfig', ['site_config' => $site_config]);
    }

    public function updateSiteConfig() {
        $site_config   = $this->request->post('site_config/a');
        $data['value'] = serialize($site_config);
        if (Db::name('system')->where('name', 'site_config')->update($data) !== false) {
            $this->success('提交成功');
        } else {
            $this->error('提交失败');
        }
    }
	
	public function editpwd(){
		return $this->fetch();
	}
	
	//更新密码
	public function updatepwd(){
		
        if ($this->request->isPost()) {	
			$rule = [
			    ['password','require','旧密码必须输入'],
			    ['xpassword','require|confirm:confirm_password','新密码必须输入|两次输入密码不一致'],
			    ['confirm_password','confirm:xpassword','两次输入密码不一致']
			];
			$data     = $this->request->post();		
			$validate = new Validate($rule);
			$result = $validate->check($data);
			if(!$result){
	    		$this->success($validate->getError());
			}
			$user = Db::name('admin_user')->field('password')->where('id='.session('admin_id'))->find();
			if($user['password'] == md5($data['password'] . Config::get('salt'))){	
				if(Db::name('admin_user')->where('id='.session('admin_id'))->setField('password',md5($data['xpassword'] . Config::get('salt')))){
					$this->success("密码修改成功");
				}else{
					$this->error("密码修改失败");
				}
			}else{
				$this->error("原密码不正确!");
			}
			
		}
		

	}

}
